<template>
  <div id="dome">
    <el-table
      :data="limits"
      style="width: 100%">
      <el-table-column type="expand" >
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand" >
            <el-form-item label="店铺名称">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="店铺地址">
              <span>{{ props.row.address }}</span>
            </el-form-item>
            <el-form-item label="店铺介绍">
              <span>{{ props.row.description }}</span>
            </el-form-item>
            <el-form-item label="店铺 ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="联系电话">
              <span>{{ props.row.phone }}</span>
            </el-form-item>
            <el-form-item label="评分">
              <span>{{ props.row.score }}</span>
            </el-form-item>
            <el-form-item label="销售量">
              <span>{{ props.row.recent_order_num}}</span>
            </el-form-item>
            <el-form-item label="分类" :cell-style="{color:'#f40'}">
              <span>{{ props.row.category }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        label="店铺名称"
        prop="name">
      </el-table-column>
      <el-table-column
        label="店铺地址"
        prop="address">
      </el-table-column>
      <el-table-column
        label="店铺介绍"
        prop="description">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
          <el-button
            size="mini"
            @click="handleAdd(scope.$index, scope.row)">添加食品</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 24px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-size="pagesize"
      layout="total, prev, pager, next, jumper"
      :total="pages">
    </el-pagination>
    <el-dialog
      title="修改店铺信息"
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleClose">
      <el-form ref="form" :model="form" size="mini" label-width="80px">
        <el-form-item label="店铺名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="店铺介绍">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="店铺分类">
          <el-cascader
            v-model="form.category"
            :options="options"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="店铺头像">
          <el-upload
            class="avatar-uploader"
            :action='baseUrl+"/statis/addimg/shop"'
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.image_path" :src="form.image_path" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-button type="primary" @click="onSubmit" size="mini" style="margin-left: 13px">确 定</el-button>

        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
      </el-form>

    </el-dialog>

  </div>
</template>

<script>
  // import data from '../../assets/data'
  import { baseImgUrl,baseUrl } from "../config/env"
  import {allShop, getAllCategory, updateShop, deleteShop} from '../api/getData'
  export default {
    async mounted(){
      this.getShop()
      this.baseUrl = baseUrl
    },
    name: 'market',
    data() {
      return {
        id:0,
        baseUrl:'',
        imageUrl: '',
        limits:[],
        pages:0,
        start:1,
        offset:0,
        currentPage4: 1,
        pagesize:12,
        dialogVisible: false,
        form: {
          name: '',
          address:'',
          description: '',
          image_path:'',
          phone:'',
          category:''
        },
        tableData: [],
        options:[],
        value:[]
      }
    },
    methods: {
      handleChange(value) {
        console.log(value);
      },
      async getShop(){
        let res = await allShop({params:{offset:this.offset,limit:this.pagesize}})
        // this.tableData = res.body;
        this.pages = res.body.count
        this.limits = res.body.result
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      async handleEdit(index, row) {
        this.id = row.id;
        this.form.name = row.name;
        this.form.address = row.address;
        this.form.phone = row.phone;
        this.form.description = row.description;
        this.form.image_path = baseImgUrl + "/shop/" + row.image_path;
        this.form.category = row.category;
        console.log(index, row);
        this.dialogVisible = true
        this.form.category = row.category.split('/')
        let res = await getAllCategory()
        res.body.result.forEach(item => {
          let data = {
            value:item.name,
            label:item.name,
            children:[]
          }
          if(item.sub_categories.length > 0){
            item.sub_categories.forEach(item => {
              let data1 = {
                value:item.name,
                label:item.name,
              }
              data.children.push(data1)
            })
          }
          this.options.push(data)

        })
        // console.log(res)
      },
      handleAdd(index, row) {
        this.$router.push({path: "/list/addfood" ,query:{restaurant_id:row.id}})
        // console.log(this.$route)
        console.log(index, row);
      },
      async handleDelete(index, row) {
        let res = await deleteShop({params: {id:row.id},credentials:true})
        if(!res.body.status){
          this.$message({
            message: res.body.message + "删除失败",
            type: 'warning'
          });
        }else{
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getShop()
        }
        console.log(res);
      },
      handleAvatarSuccess (res, file) { // 和上传图片相关
        // console.log(1)
        console.log(res,file)
        this.imageUrl = res.filename
        this.form.image_path = URL.createObjectURL(file.raw); // 和上传图片相关
      },
      beforeAvatarUpload (file) {
        // console.log(2)
        const isJPG = file.type === 'image/jpeg' || 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 10;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      async onSubmit() {
        // console.log('submit!');
        console.log(this.form)
        var res = await updateShop({id:this.id,update:this.form,imageUrl:this.imageUrl});
        console.log(res.body)
        if(!res.body.status){
          this.$message({
            message: '请填写完整',
            type: 'warning'
          });
        }else{
          this.dialogVisible = false
          this.$message({
            message: '编辑成功',
            type: 'success'
          });
          this.getShop( )
        }
        // console.log(res)
      },
      handleSizeChange(val) {

        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        // this.limits = this.tableData.slice((val-1)*this.pagesize,(val-1)*this.pagesize+this.pagesize)
        // this.start = (val-1)*this.pagesize+1
        this.offset = (val - 1) * this.pagesize
        this.getShop()
        console.log(`当前页: ${val}`);
      },
    }
  }
</script>

<style lang="less" scoped>
  .avatar-uploader .el-upload {
    /*border: 4px dashed #d9d9d9;*/
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    font-size: 28px;
    color: #8c939d;
    width: 90px;
    height: 90px;
    line-height: 90px;
    text-align: center;
  }
  .avatar {
    /*border: 4px dashed #d9d9d9;*/
    width: 90px;
    height: 90px;
    display: block;
  }
  #dome{
    /*background: #222222;*/
  .el-form--inline .el-form-item__label{
    color: #1d9cf8 !important;
    width: 60px;
  }
  tbody{
    tr{
      background: #FBFDFF!important;

    }
  }
  .el-form{
    .el-form-item{
      .el-form-item__label{
        color:#fff!important;
      }
    }
  }
  }

  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
